<?php
class DuansController extends AppController {

	var $name = 'Duans';
	var $helpers = array('Html','Ajax','Javascript','Paginator','Changetv');
	var $components = array('RequestHandler', 'Email', 'Session', 'MathCaptcha'); 
	var $paginate = array(
		'limit' => 10,
		'order' => array(
			'Duan.vitrixh' => 'asc'
		)
    );
	function index() {
		$this->layout = 'layout';
		$this->set('title', 'Thông tin dự án bất động sản Đà Nẵng');
		$this->set('keyword', 'Du an Da Nang');
		$this->set('tab', 'home');
	}
	function listvipprojects(){
		$this->layout = "detail";
		$data = $this->paginate('Duan', array('Duan.phanloai' => '1'));
		$this->set('duans', $data);
		$breadcrum = array( 
							'type' => '1',
							'tieudeduan' => '',
							'idduan' => '',
							'tieudetinrao' => '',
						  );
		$this->set('breadcrum', $breadcrum);
		$this->set('tab', 'listvip');
		$this->set('title', 'Danh sách những dự án nổi bật');
		$this->set('keyword', 'Du an Da Nang, Danh sach nhung du an noi bat');
	}
	function listnormalprojects(){
		$this->layout = "detail";
		$data = $this->paginate('Duan', array('Duan.phanloai' => '0'));
		$this->set('duans', $data);
		$breadcrum = array( 
							'type' => '0',
							'tieudeduan' => '',
							'idduan' => '',
							'tieudetinrao' => '',
						  );
		$this->set('breadcrum', $breadcrum);
		$this->set('tab', 'listnormal');
		$this->set('title', 'Danh sách những dự án khác');
		$this->set('keyword', 'Du an Da Nang, Danh sach nhung du an khac');
	}
	function admin_index() {
		$this->layout = 'admin';
		$this->Duan->recursive = 0;
		$this->set('duans', $this->paginate());
	}
	/**
	 * view all duan vip
	 */
	function changetv($str){
		$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
		"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
		,"ế","ệ","ể","ễ",
		"ì","í","ị","ỉ","ĩ",
		"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
		,"ờ","ớ","ợ","ở","ỡ",
		"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
		"ỳ","ý","ỵ","ỷ","ỹ",
		"đ",
		"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
		,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
		"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
		"Ì","Í","Ị","Ỉ","Ĩ",
		"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
		,"Ờ","Ớ","Ợ","Ở","Ỡ",
		"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
		"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
		"Đ");

		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
		,"a","a","a","a","a","a",
		"e","e","e","e","e","e","e","e","e","e","e",
		"i","i","i","i","i",
		"o","o","o","o","o","o","o","o","o","o","o","o"
		,"o","o","o","o","o",
		"u","u","u","u","u","u","u","u","u","u","u",
		"y","y","y","y","y",
		"d",
		"A","A","A","A","A","A","A","A","A","A","A","A"
		,"A","A","A","A","A",
		"E","E","E","E","E","E","E","E","E","E","E",
		"I","I","I","I","I",
		"O","O","O","O","O","O","O","O","O","O","O","O"
		,"O","O","O","O","O",
		"U","U","U","U","U","U","U","U","U","U","U",
		"Y","Y","Y","Y","Y",
		"D");
		return str_replace($marTViet,$marKoDau,$str);
	}
	
	
	
	function vipprojects(){
		//$today = date("Y-m-d");
		//$data = $this->paginate('Duan', array('Duan.phanloai' => '1', 'Duan.ngayketthuc >=' => $today));
		$data = $this->Duan->find('all', array('conditions' => array('Duan.phanloai' => 1), 'limit' => 10, 'order' => array('Duan.ngaydang' => 'desc')));
		if (isset($this->params['requested'])) {
			return $data;
        }
		$this->set('data', $data);
	}
	function normalprojects(){
		//$today = date("Y-m-d");
		//$data = $this->paginate('Duan', array('Duan.phanloai' => '0', 'Duan.ngayketthuc >=' => $today));
		$data = $this->Duan->find('all', array('conditions' => array('Duan.phanloai' => 0), 'limit' => 10, 'order' => array('Duan.vitrixh' => 'asc')));
		if (isset($this->params['requested'])) {
			return $data;
        }
		$this->set('data', $data);
	}
	function bannerprojects(){
		$data = $this->Duan->find('all', array( 'limit' => 10, 'order' => array( 'Duan.ngaydang' => 'desc'), 'conditions' => array( 'Duan.topbanner' => 1)));
		if (isset($this->params['requested'])) {
			return $data;
        }
		//$this->set('data', $data);
	}
	function view($id = null) {
		$this->layout = 'detail';
		//App::import('Vendor', 'recaptchalib');
		if (!$id) {
			$this->Session->setFlash(__('Invalid duan', true));
			$this->redirect(array('action' => 'index'));
		}
		//$tinraos = $this->Duan->Tinrao->find('all',array('order' => array('Tinrao.vitrixh ASC')));
		$this->paginate = array(
			'limit' => 15,
			'order' => array(
				'Tinrao.ngaydang' => 'desc'
			)
		);
		$tinraos = $this->paginate('Tinrao', array('Tinrao.duan_id' => $id, 'Tinrao.publish' => 1));
		$otherduans = $this->Duan->find('all',array('order' => array('Duan.ngaydang desc'), 'limit' => 10, 'conditions' => array('Duan.id !=' => $id)));
		$duan = $this->Duan->read(null, $id);
		$this->set('otherduans', $otherduans);
		$this->set('tinraos', $tinraos);
		$this->set('duan', $duan);
		$breadcrum = array( 
							'type' => $duan['Duan']['phanloai'],
							'tieudeduan' => $duan['Duan']['tieude'],
							'idduan' => '',
							'tieudetinrao' => '',
						  );
		
		$this->set('breadcrum', $breadcrum);
		//set tab
		$this->set('mathCaptcha', $this->MathCaptcha->generateEquation()); 
		if($duan['Duan']['phanloai'] == 1){
			$this->set('tab', 'listvip');
		}else if($duan['Duan']['phanloai'] == 0){
			$this->set('tab', 'listnormal');
		}
		
		$title = ''.$duan['Duan']['tieude'];
		$keyword = 'Du an Da Nang, Du an '.$duan['Duan']['tieude'].', mua ban du an '.$duan['Duan']['tieude'].', dau tu du an '.$duan['Duan']['tieude'].', thong tin du an '.$duan['Duan']['tieude'];
		$this->set('title', $title);
		$this->set('keyword', $keyword);
		
	}
	
	function search() {
		$this->layout = 'detail';
		if($_POST['query'])
		$textsearch = $this->changetv($_POST['query']);
		$this->paginate = array(
								'limit' => 10,
								'order' => array(
									'Duan.vitrixh' => 'asc'
								),
								'conditions' => array('Duan.searchable LIKE' => '%'.$textsearch.'%'),
							);
		$data = $this->paginate('Duan');
		$this->set('duans', $data);
		$breadcrum = array( 
							'type' => 2,//result search
							'tieudeduan' => '',
							'idduan' => '',
							'tieudetinrao' => '',
						  );
		$this->set('breadcrum', $breadcrum);
		$this->set('tab', '');
		$this->set('title', 'Kết quả tìm kiếm dự án');
		$this->set('keyword', 'Du an Da Nang, ket qua tim kiem du an');
	}

	/* function add() {
		$this->layout = 'admin';
		if (!empty($this->data)) {
			$this->Duan->create();
			$this->data['Duan']['searchable'] = $this->changetv($this->data['Duan']['tieude']);
			if($this->data['Duan']['vitrixh']>=1&&$this->data['Duan']['vitrixh']<=10){
				for($i=10;$i>=$this->data['Duan']['vitrixh'];$i--){
					$query = 'UPDATE duans SET vitrixh='.($i+1).' WHERE vitrixh='.$i;
					$this->Duan->query($query);
				}
			}
			if ($this->Duan->save($this->data)) {
				$this->Session->setFlash(__('The duan has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The duan could not be saved. Please, try again.', true));
			}
		}
	} */
	function change_vitrixh(){
		$idduan = $_POST['idduan'];
		$vitriedit = $_POST['vitrixh'];
		$vitri = $_POST['vitrixh'];
		
		$i = $vitriedit;
		while(1){
			$duan = $this->Duan->find('first', array('conditions' => array('Duan.vitrixh' => $i)));
			if($duan && $duan['Duan']['phanloai']==0){
				$i++;
			}else{
				break;
			}
			
		}
		$query = 'UPDATE duans SET vitrixh = '.$i.' WHERE vitrixh = '.$vitriedit;
		$this->Duan->query($query);
		$query = 'UPDATE duans SET vitrixh = '.$vitriedit.' WHERE id = '.$idduan;
		$this->Duan->query($query);
		
		$this->redirect(array('action' => 'admin_index'));
		
	}
	function change_topbanner(){
		$this->layout = 'admin';
		$idduan = $_POST['idduan'];
		$banner = $_POST['topbanner'];
		$query = 'UPDATE duans SET topbanner = '.$banner.' WHERE id = '.$idduan;
		$this->Duan->query($query);
		$this->redirect(array('action' => 'admin_index'));
	}
	function phanloai(){
		$this->layout = 'admin';
		$idduan = $_POST['idduan'];
		$loai = $_POST['phanloai'];
		$query = 'UPDATE duans SET phanloai = '.$loai.' WHERE id = '.$idduan;
		$this->Duan->query($query);
		$this->redirect(array('action' => 'admin_index'));
	}
	function admin_add() {
		$this->layout = 'admin';
		if(isset($_POST['tieude'])){
			$tieude = $_POST['tieude'];
			$noidung = $_POST['noidung'];
			$diachi = $_POST['diachi'];
			$vitrixh = $_POST['vitrixh'];
			$phanloai = $_POST['phanloai'];
			$banner = $_POST['topbanner'];
			$sdtlienlac = $_POST['sdtlienlac'];
			$mota = $_POST['mota'];
			$chudautu = $_POST['chudautu'];
			$ngayketthuc = date("Y-m-d", strtotime($_POST['ngayketthuc']));
			$ngaydang = date('Y-m-d H:m:s');
			$tinhtrang = $_POST['tinhtrang'];
			$searchable = $this->changetv($tieude);
			$target = $_SERVER['DOCUMENT_ROOT'].$this->webroot."app/webroot/upload/"; 
			$target = $target . basename( $_FILES['hinhdaidien']['name']) ; 
			
			if(isset($_FILES['hinhdaidien'])){
			/* if ((($_FILES["hinhdaidien"]["type"] == "image/gif")
			|| ($_FILES["hinhdaidien"]["type"] == "image/jpeg")
			|| ($_FILES["hinhdaidien"]["type"] == "image/pjpeg"))
			&& ($_FILES["hinhdaidien"]["size"] < 20000)){ */
				if(move_uploaded_file($_FILES['hinhdaidien']['tmp_name'], $target)) 
				{
					//echo "The file ". basename( $_FILES['hinhdaidien']['name']). " has been uploaded";
					//if (!empty($this->data) &&
					 //is_uploaded_file($this->data['Library']['File']['tmp_name'])) {
					

					
					$hinhdaidien = $_FILES['hinhdaidien']['name'];
					
					

					//$this->redirect('somecontroller/someaction');
				//}
				
				}
			}
			/* if($this->data['Duan']['vitrixh']>=1&&$this->data['Duan']['vitrixh']<=10){
				for($i=10;$i>=$this->data['Duan']['vitrixh'];$i--){
					$query = 'UPDATE duans SET vitrixh='.($i+1).' WHERE vitrixh='.$i;
					$this->Duan->query($query);
				}
			} */
			if($phanloai == 0){
				$i = $vitrixh;
				while(1){
					$duan = $this->Duan->find('first', array('conditions' => array('Duan.vitrixh' => $i, 'Duan.phanloai' => 0)));
					if($duan){
						$i++;
					}else{
						break;
					}
				}
				$query = 'UPDATE duans SET vitrixh='.$i.' WHERE vitrixh = '.$vitrixh;
				$this->Duan->query($query);	
			}
			$query_insert = 'INSERT INTO duans (tieude, noidung, diachi, vitrixh, phanloai, sdtlienlac, mota, chudautu, ngayketthuc, ngaydang, searchable, hinhdaidien, topbanner, tinhtrang)
							VALUES(\''.$tieude.'\', \''.$noidung.'\', \''.$diachi.'\', \''.$vitrixh.'\', \''.$phanloai.'\', \''.$sdtlienlac.'\', \''.$mota.'\', \''.$chudautu.'\', \''.$ngayketthuc.'\', \''.$ngaydang.'\', \''.$searchable.'\', \''.$hinhdaidien.'\', \''.$banner.'\', \''.$tinhtrang.'\')';
			if ($this->Duan->query($query_insert)) {
				$this->Session->setFlash(__('Dự án được tạo thành công', true));
				$this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Dự án chưa được tạo', true));
			}
			/* }
			else
			{
				echo "Invalid file";
			} */
		}
	}
	/* function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid duan', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Duan->save($this->data)) {
				$this->Session->setFlash(__('The duan has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The duan could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Duan->read(null, $id);
			
		}
	} */
	function admin_edit($id = null) {
		$this->layout = "admin";
		if(!isset($_POST['tieude'])
			&&!isset($_POST['noidung'])
			&&!isset($_POST['diachi'])&&!isset($_POST['vitrixh'])
			&&!isset($_POST['phanloai'])){
			//$this->data = $this->Duan->read(null, $id);
				$this->set('duan', $this->Duan->read(null, $id));
			}
		else{
			$id = $_POST['id'];
			$tieude = $_POST['tieude'];
			$noidung = $_POST['noidung'];
			$diachi = $_POST['diachi'];
			$vitrixh = $_POST['vitrixh'];
			$phanloai = $_POST['phanloai'];
			$banner = $_POST['topbanner'];
			$mota = $_POST['mota'];
			$chudautu = $_POST['chudautu'];
			$ngayketthuc = date("Y-m-d", strtotime($_POST['ngayketthuc']));
			$sdtlienlac = $_POST['sdtlienlac'];
			$ngaydang = date('Y-m-d H:m:s');
			$searchable = $this->changetv($tieude);
			$hinhdaidien = $_POST['hinhdd'];
			$tinhtrang = $_POST['tinhtrang'];
			$target = $_SERVER['DOCUMENT_ROOT'].$this->webroot."app/webroot/upload/"; 
			$target = $target . basename( $_FILES['hinhdaidien']['name']) ; 
			$ok=1; 
			if(isset($_FILES['hinhdaidien']['name'])){
			/* if ((($_FILES["hinhdaidien"]["type"] == "image/gif")
			|| ($_FILES["hinhdaidien"]["type"] == "image/jpeg")
			|| ($_FILES["hinhdaidien"]["type"] == "image/pjpeg"))
			&& ($_FILES["hinhdaidien"]["size"] < 20000)){ */
				if(move_uploaded_file($_FILES['hinhdaidien']['tmp_name'], $target)) 
				{
					//echo "The file ". basename( $_FILES['hinhdaidien']['name']). " has been uploaded";
					//if (!empty($this->data) &&
					 //is_uploaded_file($this->data['Library']['File']['tmp_name'])) {
					

					
					$hinhdaidien = $_FILES['hinhdaidien']['name'];
					
					

					//$this->redirect('somecontroller/someaction');
				//}
				
				}
			}
			if($phanloai == 0){
				$i = $vitrixh;
				while(1){
					$duan = $this->Duan->find('first', array('conditions' => array('Duan.vitrixh' => $i, 'Duan.phanloai' => 0)));
					if($duan){
						$i++;
					}else{
						break;
					}
				}
				$query = 'UPDATE duans SET vitrixh='.$i.' WHERE vitrixh = '.$vitrixh;
				$this->Duan->query($query);	
			}
			$query = 'UPDATE duans SET tieude = \''.$tieude.'\', mota = \''.$mota.'\', noidung = \''.$noidung.'\', diachi = \''.$diachi.'\', hinhdaidien = \''.$hinhdaidien.'\', vitrixh = '.$vitrixh.', phanloai = '.$phanloai.', searchable = \''.$searchable.'\', ngaydang = \''.$ngaydang.'\', chudautu = \''.$chudautu.'\', sdtlienlac = \''.$sdtlienlac.'\', ngayketthuc = \''.$ngayketthuc.'\', topbanner = \''.$banner.'\', tinhtrang = \''.$tinhtrang.'\'  WHERE id='.$id;
			if ($this->Duan->query($query)) {
				$this->Session->setFlash(__('Dự án đã được cập nhật', true));
				$this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Dự án chưa được cập nhật', true));
			}
		}
		
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không thể xóa được', true));
			$this->redirect(array('action'=>'admin_index'));
		}else{
			$da = $this->Duan->find('first', array('conditions' => array('Duan.id' => $id)));
			$i = $da['Duan']['vitrixh'] + 1;
			if ($this->Duan->delete($id)) {
				$this->Duan->Tinrao->deleteAll(array('Tinrao.duan_id' => $id));
				while(1){
					$duan = $this->Duan->find('first', array('conditions' => array('Duan.vitrixh' => $i)));
					if($duan && $duan['Duan']['phanloai'] == 0){
						$query = 'UPDATE duans SET vitrixh = '.($i-1).' WHERE vitrixh = '.$i;
						$this->Duan->query($query);
						$i++;
					}else{
						break;
					}
					
				}
				$this->Session->setFlash(__('Xóa thành công', true));
				$this->redirect(array('action'=>'admin_index'));
			}
			$this->Session->setFlash(__('Không thể xóa được', true));
			$this->redirect(array('action' => 'admin_index'));
		}
	}
	/* function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for duan', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Duan->delete($id)) {
			$this->Session->setFlash(__('Duan deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Duan was not deleted', true));
		$this->redirect(array('action' => 'index'));
	} */
	function admin_search(){
		$this->layout = "admin";
		//search by ten lien he
		if($_POST['type'] == 'diachi' || $_POST['type'] == 'tieude' || $_POST['type'] == 'noidung'){
			$this->paginate = array(
								'limit' => 25,
								'order' => array(
									'Duan.ngaydang' => 'desc'
								),
								'conditions' => array('Duan.'.$_POST['type'].' LIKE' => '%'.$_POST['inputsearch'].'%'),
							);
			$data = $this->paginate('Duan');
			 /* if($data){
				$this->Session->setFlash(__('Tìm thấy tin rao', true));
			} */
			$this->set('duans', $data);
		}else if($_POST['type'] == 'ngaydang'){
			$fromdate = date("Y-m-d H:i:s", strtotime( $_POST['fromdate']));;
			$todate = date("Y-m-d H:i:s", strtotime( $_POST['todate']));
			$this->paginate = array(
								'limit' => 25,
								'order' => array(
									'Duan.ngaydang' => 'desc'
								),
								'conditions' => array('Duan.'.$_POST['type'].' >=' => $fromdate, 'Duan.'.$_POST['type'].' <=' => $todate),
							);
			$data = $this->paginate('Duan');
			$this->set('duans', $data);
		}else if($_POST['type'] == 'phanloai'){
			$this->paginate = array(
								'limit' => 25,
								'order' => array(
									'Duan.ngaydang' => 'desc'
								),
								'conditions' => array('Duan.'.$_POST['type'] => $_POST['duantype']),
							);
			$data = $this->paginate('Duan');
			$this->set('duans', $data);
		}
	}
}
?>